package com.qeado.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.admin.model.TypeCode;
import com.qeado.admin.service.TypeCodeService;
import com.qeado.common.helper.AttributeHelper;

@Controller
public class TypeCodeController {
	@Autowired
	private TypeCodeService typeCodeService;
	
	@RequestMapping("/typeCodeManage.ac")
    public String typeCodeManage(HttpServletRequest request) {
		AttributeHelper.removeSessionAttribute(request, "T_");
        return "admin/type_code_search";
    }
	
	@RequestMapping("/newTypeCode.ac")
    public ModelAndView newTypeCode(TypeCode typeCode) {
		typeCode.setLevels(1);
        return new ModelAndView("admin/type_code_manage","typeCode",typeCode);
    }
	
	@RequestMapping("/typeCodeSearch.ac")
    public String typeCodeSearch(HttpServletRequest request, TypeCode typeCode) {
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeList(typeCode);
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_search";
    }
	
	@RequestMapping("/selectTypeCode.ac")
    public String selectTypeCode(HttpServletRequest request, String id) {
		TypeCode typeCode = typeCodeService.searchTypeCodeById(id);
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeListById(id);
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_manage";
    }
	
	@RequestMapping("/deleteTypeCode.ac")
    public String deleteTypeCode(HttpServletRequest request, String id, TypeCode typeCode) {
		typeCodeService.deleteTypeCode(id);
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeList(typeCode);
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_search";
    }
	
	@RequestMapping("/deleteTypeCodeDtl.ac")
    public String deleteTypeCodeDtl(HttpServletRequest request, TypeCode typeCode) {
		typeCodeService.deleteTypeCode(typeCode.getDtlId());
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeListById(typeCode.getId());
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_manage";
    }
	
	@RequestMapping("/saveTypeCode.ac")
    public String saveTypeCode(HttpServletRequest request, TypeCode typeCode) {
		String message = typeCodeService.saveTypeCode(typeCode);
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeListById(typeCode.getId());
		request.setAttribute("message", message);
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_manage";
    }
	
	@RequestMapping("/saveTypeCodeDtl.ac")
    public String saveTypeCodeDtl(HttpServletRequest request, TypeCode typeCode) {
		typeCodeService.saveTypeCodeDtl(request.getParameter("typeDtlName"), typeCode);
		List<TypeCode> typeCodeList = typeCodeService.searchTypeCodeListById(typeCode.getId());
		request.setAttribute("typeCode", typeCode);
		request.setAttribute("typeCodeList", typeCodeList);
        return "admin/type_code_manage";
    }

}
